<?
if (strlen(GetParam("crno","")) && strlen(GetParam("source","")) && strlen(GetParam("reccount",""))) {
// Get Parameter For Grid Form
?>
function CheckExist() {
var str_js;
var nflag;
	if (reccount>1)
		for (x=1;x<reccount;x++) {
			eval("nflag = document.<?=$HTMLFormName?>.thbl"+x+".value");			
			if ((nflag==<?=GetParam("source","")?>) && (x!=<?=GetParam("reccount","")?>)) {return false;}
		}
		return true;
}
<?
	$counter = GetParam("reccount","");
	$SQL  = "SELECT trans_type, receiptname, skada, length FROM $DBName.tblreceipt WHERE kdreceipt='".GetParam("source","")."' ";
	$Result = $DBConnection->dbc->get_row($SQL);
	$SRCType = $Result->trans_type;
	$ReceiptName = $Result->receiptname;
	$Skada = $Result->skada;
	$Length = $Result->length;
	$SQL  = "SELECT $DBCoins.coins_container_process.crno,$DBCoins.coins_container_process.cpid, ".$SRCType." as jumlah,  '1 X ".$ReceiptName."' as description, '$Skada' as skada ";
	$SQL .= "FROM $DBCoins.coins_container_process ";
	$SQL .= "INNER JOIN $DBCoins.tblprincipal ON $DBCoins.coins_container_process.cpopr=$DBCoins.tblprincipal.prcode ";	
	$SQL .= "INNER JOIN $DBCoins.tblcontract ON $DBCoins.tblprincipal.PRCONTRACTNO=$DBCoins.tblcontract.cono AND $DBCoins.tblprincipal.prcode=$DBCoins.tblcontract.prcode ";
	$SQL .= "INNER JOIN $DBCoins.tblcontainer ON $DBCoins.coins_container_process.crno=$DBCoins.tblcontainer.crno ";
	$SQL .= "INNER JOIN $DBCoins.tblcontainer_code ON $DBCoins.tblcontainer_code.cccode=$DBCoins.tblcontainer.cccode ";
	$SQL .= "WHERE $DBCoins.coins_container_process.crno='".GetParam("crno","")."' AND $DBCoins.tblcontainer_code.CCLENGTH=$Length ";
	$Result = $DBConnection->dbc->get_row($SQL);
	$JSStr  = "if (CheckExist()) { \n";
	$JSStr .= "document.$HTMLFormName.uraian_tools".$counter.".value=\"".$Result->description."\";\n";
	$JSStr .= "document.$HTMLFormName.jumlah".$counter.".value=\"".$Result->jumlah."\";\n";
	$JSStr .= "document.$HTMLFormName.skada".$counter.".value=\"".$Result->skada."\";\n";
	$JSStr .= "document.$HTMLFormName.cpid".$counter.".value=\"".$Result->cpid."\";\n";
//	$JSStr .= "AddFormGrid(document.$HTMLFormName,'$HTMLFormName.uraian_tools');\n";
	$JSStr .= "GetTotal();\n } else {\n";
	$JSStr .= "document.$HTMLFormName.thbl".$counter.".value='';\n";	
	$JSStr .= "}\n";
	print $JSStr;die();
} elseif (strlen(GetParam("crno",""))) {
	$SQL = "
			SELECT $DBCoins.`coins_container_process`.`CPCUST`,$DBCoins.`coins_container_process`.`CPOPR`,
						 $DBCoins.`coins_container_process`.`CPIVES`,
						 $DBCoins.`coins_container_process`.`CPIVOY`
			FROM
			  $DBCoins.`tblcontainer`
			  INNER JOIN $DBCoins.`coins_container_process` ON (`tblcontainer`.`CRCPID` = `coins_container_process`.`CPID`)	
			WHERE $DBCoins.`tblcontainer`.`CRNO` = '".GetParam("crno","")."'
	";
	//print $SQL;
	$res = $DBConnection->dbc->get_row($SQL);
		if ($res) {
			$js_script .= "  document.$HTMLFormName.principal.value = '".$res->CPOPR."';\n";
			$js_script .= "  document.$HTMLFormName.customer.value = '".$res->CPICUST."';\n";
			$js_script .= "  document.$HTMLFormName.do_number.value = '".$res->CPIORDERNO."';\n";
			$js_script .= "  document.$HTMLFormName.vessel.value = '".$res->CPIVES."';\n";
			$js_script .= "  document.$HTMLFormName.voyage.value = '".$res->CPIVOY."';\n";
	  } else {
	  	$js_script .= "alert('Container number not found!');\n";
	  	$js_script .= "document.$HTMLFormName.container_no.value = '".$res->CPIOPR."';\n";
	  }
		print $js_script;
		die();
}
if (GetParam("debtur","")) {
	$SQL = "SELECT * FROM idms_general.tbldebitur WHERE CUCODE = '".GetParam("debtur","")."'";
//	print $SQL;
	$Result = $DBConnection->dbc->get_row($SQL);
	if ($Result) {
		$js_script .= "  document.".$HTMLFormName.".debtur.value = '".$Result->CUCODE."';\n";
		$js_script .= "  document.".$HTMLFormName.".debnam.value = '".$Result->CUNAME ."';\n";
		$js_script .= "  document.".$HTMLFormName.".npwp.value   = '".$Result->CUNPWP."';\n";
		$js_script .= "  document.".$HTMLFormName.".addr.value   = '".$Result->CUADDR."';\n";
		print $js_script;
	}
	die();	
}
?>
<html>
<script type="text/javascript" src="<?=$ROOT_JS_DIR?>/common.js"></script>
<script type="text/javascript" src="<?=$ROOT_JS_DIR?>/jsmasks.js"></script>
<body>
<h1>Loading data...</h1>
<?
define("PRIMARY_KEY","nik");
$FileName       = "ais_transaction_receipt_container_rpc.php";

if (GetParam("tglbkt","") && GetParam("curr","")) {
	list($day,$month,$year) = explode("/",GetParam("tglbkt",""));
	$SQL = "SELECT rate FROM ais_kurssi WHERE tglbkt <= '$year-$month-$day' AND curr='".GetParam("curr","")."' ORDER BY tglbkt DESC LIMIT 1";
//	print $SQL;
	$rate = $DBConnection->dbc->get_var($SQL);
	if ($rate) {
		$js_script  = "<script>\n";
		$js_script .= "  parent.document.".$HTMLFormName.".rate.value = '$rate';\n";		
		$js_script .= "  oMask = new Mask('#,###.#0', 'number');\n";
		$js_script .= "  parent.document.".$HTMLFormName.".rate.value = oMask.format(parent.document.$HTMLFormName.rate.value);\n";
		$js_script .= "</script>\n";
		print $js_script;		
	} else {
		$js_script  = "<script>\n";
		$js_script .= "  parent.document.".$HTMLFormName.".rate.value = '0';\n";		
		$js_script .= "  oMask = new Mask('#,###.#0', 'number');\n";
		$js_script .= "  parent.document.".$HTMLFormName.".rate.value = oMask.format(parent.document.$HTMLFormName.rate.value);\n";
		$js_script .= "</script>\n";		
		print $js_script;		
	}
} elseif (GetParam("thbl","") && GetParam("idx","")) {
	$SQL   = "SELECT skada,receiptname FROM tblreceipt WHERE kdreceipt=".GetParam("thbl","");
	$res = $DBConnection->dbc->get_row($SQL);
	if (strlen($skada)) {
		$js_script  = "<script>\n";
		$js_script .= "  parent.document.$HTMLFormName.skada".GetParam("idx","").".value = '".$res->skada."'\n";
		$js_script .= "  parent.document.$HTMLFormName.uraian".GetParam("idx","").".value = '".$res->receiptname."'";
		$js_script .= "</script>\n";
	} else {
		$js_script  = "<script>\n";
		$js_script .= "  parent.document.$HTMLFormName.skada".GetParam("idx","").".value = '".$res->skada."'\n";
		$js_script .= "  parent.document.$HTMLFormName.uraian".GetParam("idx","").".value = '".$res->receiptname."'";
		$js_script .= "</script>\n";		
	}
	print $js_script;
} elseif (GetParam("ledger","") && GetParam("trsto","")) {
		list($day,$month,$year) = explode("/",GetParam("ledger",""));
	    $SQL   = "SELECT count(*) FROM ais_history_ledger WHERE month='$month' AND year = '$year' AND status='open'";
	    $ExistPeriod = $DBConnection->dbc->get_var($SQL);
		if (!$ExistPeriod) {
			$js_script  = "<script>\n";
			$js_script .= "  parent.document.$HTMLFormName.".GetParam("trsto","").".value = '';\n";
			$js_script .= "  alert('Invalid Date, Period Allready Closed!');\n";
			$js_script .= "  parent.document.$HTMLFormName.".GetParam("trsto","").".focus();\n";
			$js_script .= "</script>\n";
	    }
	print $js_script;	    
}

?>